
package ch.hearc.coursJava.kitBase.tableau.equationOrdre2;

import com.bilat.tools.io.console.Clavier;

public class EquationOrdre2
	{

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	/**
	 * <pre>
	 * tab[0] = a
	 * tab[1] = b
	 * tab[2] = c
	 * </pre>
	 */
	public static double[] saisirCoefficients()
		{
		double[] tabCoefficients = new double[3];

		do
			{
			tabCoefficients[0] = Clavier.lireDouble("a = ");
			} while(tabCoefficients[0] == 0);

		tabCoefficients[1] = Clavier.lireDouble("b = ");
		tabCoefficients[2] = Clavier.lireDouble("c = ");

		return tabCoefficients;
		}

	public static double discriminant(double[] tabCoefficients)
		{
		double a = tabCoefficients[0];
		double b = tabCoefficients[1];
		double c = tabCoefficients[2];

		return b * b - 4 * a * c;
		}

	/**
	 * Renvoie null si pas de solution
	 */
	public static double[] solve(double[] tabCoefficients)
		{
		double discriminant = discriminant(tabCoefficients);
		double a = tabCoefficients[0];
		double b = tabCoefficients[1];

		if (discriminant > 0)
			{
			double[] tabReponse = new double[2];

			tabReponse[0] = (-b + Math.sqrt(discriminant)) / (2*a);
			tabReponse[1] = (-b - Math.sqrt(discriminant)) / (2*a);

			return tabReponse;
			}
		else if (discriminant == 0)
			{
			double[] tabReponse = new double[1];

			tabReponse[0] = -b / (2*a);

			return tabReponse;
			}
		else
			{
			return null;
			}
		}

	public static double[] solve(double a,double b,double c)
		{
		double[] tab=new double[]{a,b,c};
		return solve(tab);
		}

	public static void afficher(double[] tabReponse)
		{
		if(tabReponse != null)
			{
			// Version 1 avec indice
//			for(int i = 1; i <= tabReponse.length; i++)
//				{
//				System.out.println("x"+i+" = "+tabReponse[i-1]);
//				}

			// Version 2 avec foreach
			int i=1;
			for(double x:tabReponse)
				{
				System.out.println("x"+i+" = "+x);
				i++;
				}
			}
		else
			{
			System.out.println("Pas de solutions !");
			}
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	}
